package com.cskaoyan.mall.controller.admin;

import com.cskaoyan.mall.model.bo.order.ChangeOrderBo;
import com.cskaoyan.mall.model.bo.order.OrderMessageBo;
import com.cskaoyan.mall.model.vo.order.*;
import com.cskaoyan.mall.service.lmpl.OrderServiceImpl;
import com.cskaoyan.mall.utils.Json;
import com.cskaoyan.mall.utils.OrderRestult;
import com.cskaoyan.mall.utils.Result;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName: OrderServlet
 * @Description: TODO
 * @Auther 庄左
 * @Date 2022/7/1 15:35
 * @Version
 **/
@WebServlet("/api/admin/order/*")
public class OrderServlet extends HttpServlet {

    //创建gson
    private Gson gson = new Gson();
    //创建用于返回响应的对象
    private Result result = new Result();
    //创建Service的引用
    private OrderServiceImpl orderService = new OrderServiceImpl();
    private Json json = new Json();


    //-----------------------------------------POST-----------------------//
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String replace = requestURI.replace(req.getContextPath() + "/api/admin/order/", "");

        if ("ordersByPage".equals(replace)) {
            ordersBypage(req, resp);
        }
        if ("changeOrder".equals(replace)) {
            changeOrder(req, resp);
        }


    }

    /**
    * @Author: 庄左
    * @Description: 订单的保存修改
    * @DateTime: 2022/7/2 22:18
    */
    private void changeOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ChangeOrderBo changeOrderBo = gson.fromJson(json.getJsonString(req), ChangeOrderBo.class);
        // 这里的getSpec获得的是id,下面的getState也是一样
        String spec = orderService.selectSpecById(changeOrderBo.getSpec());
        String state = orderService.selectStateById(changeOrderBo.getState());

        changeOrderBo.setStateValue(state);
        changeOrderBo.setSpecValue(spec);
        orderService.changeOrder(changeOrderBo);
        result.setCode(0);
        resp.getWriter().println(gson.toJson(result));

    }

    private void ordersBypage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取订单信息返回给页面
        //去数据库中查找订单,查找所有不需要传入对象，考虑实现条件查询，这里传入订单对象，传入null即为查询所有订单

        // 获取请求体的Java对象形式
        String reqBody = json.getJsonString(req);
        OrderMessageBo orderMessageBo = gson.fromJson(reqBody, OrderMessageBo.class);

        //接收响应体的对象
        //接收order
        OrderRestult orderRestult = new OrderRestult();
        //order
        List<OrderMessageVo> order = orderService.selectOrderMessage(orderMessageBo);
        //usermessage
        for (OrderMessageVo messageVo : order) {
            Integer orderId = messageVo.getId();
            Integer id = messageVo.getUserId();
            UserOrderMessageVo user = orderService.selectUserMessage(id,orderId);
            messageVo.setUserMessageVo(user);
        }
        //total
        orderRestult.setTotal(order.size());
        orderRestult.setOrderMessage(order);
        //最终的响应体
        result.setCode(0);
        result.setData(orderRestult);
        resp.getWriter().println(gson.toJson(result));

    }

    //---------------------------GET------------------------------------//


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String replace = requestURI.replace(req.getContextPath() + "/api/admin/order/", "");
        if ("order".equals(replace)) {
            order(req, resp);
        }
        if("deleteOrder".equals(replace)){
            deleteOrder(req,resp);
        }

    }
// 删除订单
    private void deleteOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Integer id = Integer.valueOf(req.getParameter("id"));

        orderService.deleteOrder(id);

        result.setCode(0);
        resp.getWriter().println(gson.toJson(result));
    }

    //编辑页面的回显
    private void order(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Integer id = Integer.valueOf(req.getParameter("id"));
        //获取订单和商品的id
        // Integer id = orderUpdate.getId();
        // Integer goodsId = orderUpdate.getGoodsDetailId();
        //创建接收对象


        OrderUpdate orderUpdate = orderService.selectorderUpdate(id);
        // 当前规格
        CurSpec curSpec = orderService.selectCurSpec(id);
        //当前发货状态
        CurState curState = orderService.selectCurState(id);

        Integer integer = orderService.selectGoodsIdByOrderId(id);
        //TODO:改bug,先查出商品id---根据订单id去数据库查询,然后根据商品id查询规格列表----完成
        List<OrderSpec> spec = orderService.selectSpecList(integer);


        //查询发货状态列表
        List<OrderStates> states = orderService.selectStateList();

        orderUpdate.setCurSpec(curSpec);

        orderUpdate.setCurState(curState);

        orderUpdate.setSpec(spec);

        orderUpdate.setStates(states);

        result.setData(orderUpdate);
        result.setCode(0);
        resp.getWriter().println(gson.toJson(result));


    }
}
